package com.lxs.doc.rocess.util;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.lxs.doc.rocess.xmind.XmindParser;
import com.lxs.doc.rocess.xmind.pojo.Attached;
import com.lxs.doc.rocess.xmind.pojo.JsonRootBean;
import com.lxs.doc.rocess.xmind.pojo.RootTopic;
import org.apache.commons.lang.StringUtils;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 解析xmind为html工具类
 */
public class XMindToHtmlConverter2 {
    public static void main(String[] args) throws Exception {
       convertXMindToHtml();
    }
    public static String convertXMindToHtml() throws Exception {
        String fileName = "D:\\360MoveData\\Users\\14046\\Desktop\\薪福宝-培训二期.xmind";
        String pathout = "D:\\360MoveData\\Users\\14046\\Desktop\\DDDDDXMIND.html";
        FileOutputStream fos = new FileOutputStream(pathout);
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String entryName = entry.getName();
            System.out.println("entryName = " + entryName);


        }
        //1,获取xmind文件的html头部
        StringBuilder htmlBuilder = getXmindHead();


        // 获取第一个sheet
//        ISheet sheet = workbook.getPrimarySheet();

        // 获取根主题 - 替代getOwnedTopicList()
        JsonRootBean parseObject = (JsonRootBean) XmindParser.parseObject(fileName);
        RootTopic rootTopic = parseObject.getRootTopic();

        htmlBuilder.append("<div class='mindmap'>");
        if (rootTopic != null) {
            htmlBuilder.append("<ul>");
            htmlBuilder.append("<li class='node' onclick ='toggleChildren(this)'>");

            // 添加中心主题
            htmlBuilder.append("<div class='node-content'>")
                    .append(rootTopic.getTitle())
                    .append("</div>");


            // 获取所有主分支
            List<Attached> attached = rootTopic.getChildren().getAttached();
            List<Attached> mainTopics = attached;

            for (Attached mainTopic : mainTopics) {
                htmlBuilder.append("<ul>");
                htmlBuilder.append("<li class=\"node\" onclick ='toggleChildren(this)'>");
                // 添加主主题
                htmlBuilder.append("<div class='node-content' ")
                        .append("style="+loadStyle(mainTopic)+">")
                        .append(mainTopic.getTitle())
                        .append("</div>");

                // 获取子主题
                List<Attached> subTopics = mainTopic.getChildren().getAttached();

                doForeachAttach(htmlBuilder, subTopics);
                htmlBuilder.append("</li>");
                htmlBuilder.append("</ul>");
            }


            htmlBuilder.append("</li");

            htmlBuilder.append("<ul>");

        }

        htmlBuilder.append("</div>");
        htmlBuilder.append("</body></html>");
        System.out.println("htmlBuilder.toString() = " + htmlBuilder.toString());
        fos.write(htmlBuilder.toString().getBytes());
        return htmlBuilder.toString();
    }

    private static StringBuilder getXmindHead() {
        String jsPath = "D:\\Downloads\\mashibing\\mall\\doc-process\\src\\main\\resources\\xmind.js";
        StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.append("<!DOCTYPE html><html><head>");
        htmlBuilder.append("<meta charset='UTF-8'>");
        htmlBuilder.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");

        htmlBuilder.append("<style>").append(loadCssTemplate()).append("</style>");
        htmlBuilder.append("<script src="+jsPath+"></script>");

        htmlBuilder.append("</head>");
        htmlBuilder.append("<body>");
        return htmlBuilder;
    }

    private static String loadStyle(Attached mainTopic) {
        if(Objects.nonNull(mainTopic.getStyle())&&StringUtils.isNotEmpty(mainTopic.getStyle().getProperties())){
            return mainTopic.getStyle().getProperties()
                    .replace("{","")
                    .replace("}","")
                    .replace(",",";");
        }
        return "";
    }

    private static void doForeachAttach(StringBuilder htmlBuilder, List<Attached> subTopics) {
        for (Attached subTopic : subTopics) {
            htmlBuilder.append("<ul>");
            htmlBuilder.append("<li  class=\"node\" onclick ='toggleChildren(this)'>");
            // 添加子主题
            htmlBuilder.append("<div class='node-content' ")
                    .append("style="+loadStyle(subTopic)+">")
                    .append(subTopic.getTitle())
                    .append("</div>");
            if(subTopic.getChildren()!=null&&CollectionUtils.isNotEmpty(subTopic.getChildren().getAttached())){
                List<Attached> attacheds = subTopic.getChildren().getAttached();
                doForeachAttach(htmlBuilder,attacheds);
            }
            htmlBuilder.append("</li>");
            htmlBuilder.append("</ul>");
        }
    }

    private static String loadCssTemplate2(){
        String style =".node-content {\n" +
                "  border: 1px solid #ccc;\n" +
                "  border-radius: 6px;\n" +
                "  padding: 8px 12px;\n" +
                "  margin: 6px;\n" +
                "  box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n" +
                "}\n" +
                " \n" +
                "/* 层级样式 */\n" +
                ".mindmap ul {\n" +
                "  list-style: none;\n" +
                "  padding-left: 20px;\n" +
                "  position: relative;\n" +
                "}\n" +
                " \n" +
                "/* 连接线样式 */\n" +
                ".mindmap li::before {\n" +
                "  content: \"\";\n" +
                "  position: absolute;\n" +
                "  left: -12px;\n" +
                "  top: 50%;\n" +
                "  border-left: 1px solid #999;\n" +
                "  height: calc(100% + 12px);\n" +
                "}\n"+
                "body { background-color: #f0f0f0; font-family: 'Arial', sans-serif; " +
                "}";
        return style;
    }
    private static String loadCssTemplate() {
        String style ="* {\n" +
                "            box-sizing: border-box;\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;\n" +
                "        }\n" +
                "        \n" +
                "        body {\n" +
                "            background-color: #f5f7fa;\n" +
                "            color: #333;\n" +
                "            line-height: 1.6;\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "        \n" +
                "        .container {\n" +
                "            max-width: 1400px;\n" +
                "            margin: 0 auto;\n" +
                "            background: white;\n" +
                "            border-radius: 10px;\n" +
                "            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);\n" +
                "            padding: 30px;\n" +
                "            display: grid;\n" +
                "            grid-template-columns: 1fr 2fr;\n" +
                "            gap: 30px;\n" +
                "        }\n" +
                "        \n" +
                "        header {\n" +
                "            grid-column: 1 / -1;\n" +
                "            text-align: center;\n" +
                "            margin-bottom: 30px;\n" +
                "        }\n" +
                "        \n" +
                "        h1 {\n" +
                "            color: #2c3e50;\n" +
                "            margin-bottom: 10px;\n" +
                "        }\n" +
                "        \n" +
                "        .description {\n" +
                "            color: #7f8c8d;\n" +
                "            margin-bottom: 20px;\n" +
                "        }\n" +
                "        \n" +
                "        .upload-section {\n" +
                "            background: #f8f9fa;\n" +
                "            padding: 25px;\n" +
                "            border-radius: 8px;\n" +
                "            margin-bottom: 30px;\n" +
                "            text-align: center;\n" +
                "            border: 2px dashed #ddd;\n" +
                "            transition: all 0.3s;\n" +
                "        }\n" +
                "        \n" +
                "        .upload-section:hover {\n" +
                "            border-color: #3498db;\n" +
                "        }\n" +
                "        \n" +
                "        .upload-btn {\n" +
                "            background: #3498db;\n" +
                "            color: white;\n" +
                "            padding: 12px 24px;\n" +
                "            border: none;\n" +
                "            border-radius: 5px;\n" +
                "            cursor: pointer;\n" +
                "            font-size: 16px;\n" +
                "            transition: background 0.3s;\n" +
                "            display: inline-block;\n" +
                "            margin: 15px 0;\n" +
                "        }\n" +
                "        \n" +
                "        .upload-btn:hover {\n" +
                "            background: #2980b9;\n" +
                "        }\n" +
                "        \n" +
                "        #file-name {\n" +
                "            margin-top: 10px;\n" +
                "            font-style: italic;\n" +
                "            color: #7f8c8d;\n" +
                "        }\n" +
                "        \n" +
                "        .action-buttons {\n" +
                "            display: flex;\n" +
                "            justify-content: center;\n" +
                "            gap: 15px;\n" +
                "            margin: 20px 0;\n" +
                "        }\n" +
                "        \n" +
                "        .btn {\n" +
                "            padding: 10px 20px;\n" +
                "            border: none;\n" +
                "            border-radius: 5px;\n" +
                "            cursor: pointer;\n" +
                "            font-size: 15px;\n" +
                "            transition: all 0.3s;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-primary {\n" +
                "            background: #2ecc71;\n" +
                "            color: white;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-primary:hover {\n" +
                "            background: #27ae60;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-secondary {\n" +
                "            background: #95a5a6;\n" +
                "            color: white;\n" +
                "        }\n" +
                "        \n" +
                "        .btn-secondary:hover {\n" +
                "            background: #7f8c8d;\n" +
                "        }\n" +
                "        \n" +
                "        .status {\n" +
                "            text-align: center;\n" +
                "            margin: 20px 0;\n" +
                "            padding: 10px;\n" +
                "            border-radius: 5px;\n" +
                "        }\n" +
                "        \n" +
                "        .status.success {\n" +
                "            background: #d4edda;\n" +
                "            color: #155724;\n" +
                "        }\n" +
                "        \n" +
                "        .status.error {\n" +
                "            background: #f8d7da;\n" +
                "            color: #721c24;\n" +
                "        }\n" +
                "        \n" +
                "        .style-panel {\n" +
                "            background: #f9f9f9;\n" +
                "            padding: 20px;\n" +
                "            border-radius: 8px;\n" +
                "            height: fit-content;\n" +
                "        }\n" +
                "        \n" +
                "        .style-panel h3 {\n" +
                "            margin-bottom: 20px;\n" +
                "            color: #2c3e50;\n" +
                "            border-bottom: 2px solid #3498db;\n" +
                "            padding-bottom: 10px;\n" +
                "        }\n" +
                "        \n" +
                "        .style-group {\n" +
                "            margin-bottom: 20px;\n" +
                "        }\n" +
                "        \n" +
                "        .style-group h4 {\n" +
                "            margin-bottom: 10px;\n" +
                "            color: #34495e;\n" +
                "        }\n" +
                "        \n" +
                "        .color-picker {\n" +
                "            display: flex;\n" +
                "            align-items: center;\n" +
                "            margin-bottom: 10px;\n" +
                "        }\n" +
                "        \n" +
                "        .color-picker label {\n" +
                "            width: 120px;\n" +
                "            display: inline-block;\n" +
                "        }\n" +
                "        \n" +
                "        .color-picker input[type=\"color\"] {\n" +
                "            width: 40px;\n" +
                "            height: 40px;\n" +
                "            border: none;\n" +
                "            border-radius: 4px;\n" +
                "            cursor: pointer;\n" +
                "        }\n" +
                "        \n" +
                "        .slider {\n" +
                "            display: flex;\n" +
                "            align-items: center;\n" +
                "            margin-bottom: 10px;\n" +
                "        }\n" +
                "        \n" +
                "        .slider label {\n" +
                "            width: 120px;\n" +
                "            display: inline-block;\n" +
                "        }\n" +
                "        \n" +
                "        .slider input {\n" +
                "            flex: 1;\n" +
                "        }\n" +
                "        \n" +
                "        .result-section {\n" +
                "            background: white;\n" +
                "            padding: 20px;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);\n" +
                "            overflow: auto;\n" +
                "            max-height: 80vh;\n" +
                "        }\n" +
                "        \n" +
                "        .mindmap-container {\n" +
                "            min-height: 500px;\n" +
                "            padding: 20px;\n" +
                "            background: white;\n" +
                "            border: 1px solid #ddd;\n" +
                "            border-radius: 5px;\n" +
                "        }\n" +
                "        \n" +
                "        .mindmap {\n" +
                "            position: relative;\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "        \n" +
                "        .central-topic {\n" +
                "            background: #3498db;\n" +
                "            color: white;\n" +
                "            padding: 15px 20px;\n" +
                "            border-radius: 8px;\n" +
                "            font-weight: bold;\n" +
                "            display: inline-block;\n" +
                "            margin-bottom: 30px;\n" +
                "            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);\n" +
                "            text-align: center;\n" +
                "        }\n" +
                "        \n" +
                "        .main-topic {\n" +
                "            background: #2ecc71;\n" +
                "            color: white;\n" +
                "            padding: 12px 18px;\n" +
                "            border-radius: 6px;\n" +
                "            margin: 15px;\n" +
                "            display: inline-block;\n" +
                "            box-shadow: 0 3px 5px rgba(0, 0, 0, 0.1);\n" +
                "        }\n" +
                "        \n" +
                "        .subtopic {\n" +
                "            background: #f1c40f;\n" +
                "            color: #333;\n" +
                "            padding: 10px 15px;\n" +
                "            border-radius: 5px;\n" +
                "            margin: 10px;\n" +
                "            display: inline-block;\n" +
                "            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);\n" +
                "        }\n" +
                "        \n" +
                "        .code-sample {\n" +
                "            background: #2c3e50;\n" +
                "            color: #ecf0f1;\n" +
                "            padding: 20px;\n" +
                "            border-radius: 8px;\n" +
                "            margin-top: 30px;\n" +
                "            overflow-x: auto;\n" +
                "            grid-column: 1 / -1;\n" +
                "        }\n" +
                "        \n" +
                "        .code-sample h3 {\n" +
                "            margin-bottom: 15px;\n" +
                "            color: #3498db;\n" +
                "        }\n" +
                "        \n" +
                "        pre {\n" +
                "            white-space: pre-wrap;\n" +
                "            line-height: 1.5;\n" +
                "        }\n" +
                "        \n" +
                "        footer {\n" +
                "            text-align: center;\n" +
                "            margin-top: 40px;\n" +
                "            color: #7f8c8d;\n" +
                "            font-size: 14px;\n" +
                "            grid-column: 1 / -1;\n" +
                "        }\n" +
                "        \n" +
                "        @media (max-width: 1024px) {\n" +
                "            .container {\n" +
                "                grid-template-columns: 1fr;\n" +
                "            }\n" +
                "        }\n"+".node-content {\n" +
                "  border: 1px solid #ccc;\n" +
                "  border-radius: 6px;\n" +
                "  padding: 8px 12px;\n" +
                "  margin: 6px;\n" +
                "  box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n" +
                "}\n" +
                " \n" +
                "/* 层级样式 */\n" +
                ".mindmap ul {\n" +
                "  list-style: none;\n" +
                "  padding-left: 20px;\n" +
                "  position: relative;\n" +
                "}\n" +
                " \n" +
                "/* 连接线样式 */\n" +
                ".mindmap li::before {\n" +
                "  content: \"\";\n" +
                "  position: absolute;\n" +
                "  left: -12px;\n" +
                "  top: 50%;\n" +
                "  border-left: 1px solid #999;\n" +
                "  height: calc(100% + 12px);\n" +
                "}\n"+
                "body { background-color: #f0f0f0; font-family: 'Arial', sans-serif; " +
                "}";
        return style;
    }

}
